//
//  CaptchaView.m
//  LocalCaptcha
//
//  Created by 李禹 on 15/5/26.
//  Copyright (c) 2015年 dayu. All rights reserved.
//

#import "CaptchaView.h"
#define kRandomColor  [UIColor colorWithRed:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:1.0]
#define kLineCount 6
#define kLineWidth 1.0
#define kCharCount 6
#define kFontSize [UIFont systemFontOfSize:arc4random() % 5 + 15]

@implementation CaptchaView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.layer.cornerRadius = 5.0;
        self.layer.masksToBounds = YES;
        self.backgroundColor = kRandomColor;
        
        //显示一个随机验证码
        [self changeCaptcha];
    }
    return self;
}

#pragma mark - 更换验证码
- (void)changeCaptcha {
    self.changeArray = [[NSArray alloc] initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z",nil];
    
    NSMutableString *getStr = [[NSMutableString alloc] initWithCapacity:kCharCount];
    self.changeString = [[NSMutableString alloc] initWithCapacity:kCharCount];
    
    for (NSInteger i = 0; i < kCharCount; i++) {
        NSInteger index = arc4random() % self.changeArray.count;
        getStr = [self.changeArray objectAtIndex:index];
        [self.changeString appendString:getStr];
    }
}

#pragma mark - 点击View时进行刷新
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self changeCaptcha];
    //setNeedsDisplay调用drawRect方法来实现view的绘制
    [self setNeedsDisplay];
}

#pragma mark - 绘制界面
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    self.backgroundColor = kRandomColor;
    
    NSString *text = [NSString stringWithFormat:@"%@", self.changeString];
    CGSize cSize = [@"S" sizeWithAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:20]}];
    NSInteger width = rect.size.width / text.length - cSize.width;
    NSInteger height = rect.size.height - cSize.height;
    
    CGPoint point = CGPointZero;
    //依次绘制每一个字符,可以设置显示的每个字符的字体大小、颜色、样式等
    CGFloat pX, pY;
    for (NSInteger i = 0; i < text.length; i++) {
        pX = arc4random() % width + rect.size.width / text.length * i;
        pY = arc4random() % height;
        point = CGPointMake(pX, pY);
        unichar c = [text characterAtIndex:i];
        NSString *textC = [NSString stringWithFormat:@"%C", c];
        
        [textC drawAtPoint:point withAttributes:@{NSFontAttributeName : kFontSize}];
    }
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, kLineWidth);
    
    for (NSInteger i = 0; i < kLineCount; i++) {
        UIColor *color = kRandomColor;
        CGContextSetStrokeColorWithColor(context, color.CGColor);
        
        pX = arc4random() % (NSInteger)rect.size.width;
        pY = arc4random() % (NSInteger)rect.size.height;
        CGContextMoveToPoint(context, pX, pY);
        
        pX = arc4random() % (NSInteger)rect.size.width;
        pY = arc4random() % (NSInteger)rect.size.height;
        CGContextAddLineToPoint(context, pX, pY);
        
        CGContextStrokePath(context);
    }
}


@end
